package com.open.cloud.rbac.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wangzengzhou
 * @since 2021/11/17
 */
public class DateTimeUtils {

    public static void beforeOneHourToNowDate() {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中小时 */
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        /* MINUTE 指示一天中的某分 */
        calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE) - 20));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("一个小时前的时间：" + df.format(calendar.getTime()));
        System.out.println("当前的时间：" + df.format(new Date()));
    }


    /**
     * 获取当前小时
     *
     * @param ihour
     * @return
     */
    public static String getBeforeHourTimeStr(int ihour) {
        String returnstr = "";
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        returnstr = df.format(calendar.getTime());
        return returnstr;
    }

    /**
     * 获取当前小时
     *
     * @param ihour
     * @return
     */
    public static Date getHourWithCurrentHour(int ihour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date hourDate = calendar.getTime();
        return hourDate;
    }

    public static String getBeforeHourTimeStr(Date dateTime, int ihour) {
        String returnstr = "";
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.setTime(dateTime);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        returnstr = df.format(calendar.getTime());
        return returnstr;
    }

    public static Date getBeforeHourTime(Date dateTime, int ihour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date hourDate = calendar.getTime();
        return hourDate;
    }


    /**
     * 获取当前月第一天
     *
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayStr = sdf.format(calendar.getTime()) + " 00:00:00";
        return firstDayStr;
    }

    /**
     * 获取当前月最后一天
     *
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = 0;
        //2月的平年瑞年天数
        if (month == 2) {
            // 这个api在计算2020年2月的过程中有问题
            lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
        } else {
            lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayStr = sdf.format(calendar.getTime()) + " 23:59:59";
        return lastDayStr;
    }

    public final static int getMonthDayNum(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = 0;
        //2月的平年瑞年天数
        if (month == 2) {
            // 这个api在计算2020年2月的过程中有问题
            lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
        } else {
            lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        return lastDay;
    }

    /**
     * 指定日期，往后加一天
     *
     * @param today
     * @return
     */
    public static String addOneday(String today) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d = new Date(f.parse(today).getTime() + 24 * 3600 * 1000);
            return f.format(d) + " 00:00:00";
        } catch (Exception ex) {
            return "输入格式错误";
        }
    }

    /**
     * 指定日期，往后num 年
     *
     * @param date
     * @return
     */
    public static Date getOtherYear(Date date, int num) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //获取一个Calendar对象
        Calendar cd = Calendar.getInstance();
//        try {
        //设置calendar日期
        cd.setTime(date);
//        } catch (Exception ex) {
//            return "输入格式错误";
//        }
        //增加num年
        cd.add(Calendar.YEAR, num);
        Date afterDate = cd.getTime();
        return afterDate;
    }


    public final static Date getOtherDay(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, num);
        date = calendar.getTime();
        return date;
    }

    public final static Date getOtherMonth(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, num);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取时间差
     *
     * @param startDate
     * @return
     * @throws ParseException
     */
    public final static int getHourToNow(String startDate, String endDate) throws ParseException {
        //如2016-08-10 20:40
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH");
        long from = simpleFormat.parse(startDate).getTime();
        long to = simpleFormat.parse(endDate).getTime();
        int hours = (int) ((to - from) / (1000 * 60 * 60));
        return hours;
    }

    /**
     * 使用java 8的Period的对象计算两个LocalDate对象的时间差，严格按照年、月、日计算，如：2018-03-12 与 2014-05-23 相差 3 年 9 个月 17 天
     *
     * @param startDateStr
     * @param endDateStr
     */
    public static Period calculateTimeDifferenceByPeriod(String startDateStr, String endDateStr) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate startDate = LocalDate.parse(startDateStr, fmt);
        LocalDate endDate = LocalDate.parse(endDateStr, fmt);
        Period p = Period.between(startDate, endDate);
        System.out.printf("目标日期距离今天的时间差：%d 年 %d 个月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());
        return p;
    }

    /**
     * 计算不足一小时剩余多少秒
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long calcSecondsDiffByDate(Date startDate, Date endDate) {
        // 日期格式化
        if (ObjectUtils.isEmpty(startDate)) {
            startDate = new Date();
        }
        if (ObjectUtils.isEmpty(endDate)) {
            endDate = new Date();
        }
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
//        int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
        long seconds = ((endTime - startTime) / 1000) % 3600;
        long diffSeconds = 3600 - seconds;
        return diffSeconds;
    }

    /**
     * 计算日期差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer calcDaysByDate(Date startDate, Date endDate) {
        // 日期格式化
        if (ObjectUtils.isEmpty(startDate)) {
            startDate = new Date();
        }
        if (ObjectUtils.isEmpty(endDate)) {
            endDate = new Date();
        }
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
        return days;
    }


    /**
     * 计算小时差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer calcHoursByDate(Date startDate, Date endDate) {
        // 日期格式化
        if (ObjectUtils.isEmpty(startDate)) {
            startDate = new Date();
        }
        long startTime = startDate.getTime();
        if (ObjectUtils.isEmpty(endDate)) {
            endDate = new Date();
        }
        long endTime = endDate.getTime();
        float hours = (float) ((endTime - startTime) / (1000 * 60 * 60.0));
        double ceil = Math.ceil(hours);
        System.out.println("number:" + hours);
        System.out.println("ceil:" + ceil);
        Integer ceilRes = new Double(ceil).intValue();
        return ceilRes;
    }

    public static String getCurrentQuarterTimeStr(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int minute = localDateTime.getMinute();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
        calendar.set(Calendar.MINUTE, (minute / 10) * 10);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }

    public static Date getCurrentQuarterTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int minute = localDateTime.getMinute();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
        calendar.set(Calendar.MINUTE, (minute / 10) * 10);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        return date1;
    }

    public static String getCurrentHourTimeStr(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int minute = localDateTime.getMinute();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }

    public static Date getCurrentHourTime(Date date) {
//        Instant instant = date.toInstant();
//        ZoneId zoneId = ZoneId.systemDefault();
//        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        int minute = localDateTime.getMinute();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        return date1;
    }

    public static String getCurrentMonthStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        String dateStr = sdf.format(date1);
        return dateStr;
    }


    public static Date getCurrentMinuteTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        int minute = localDateTime.getMinute();
        int hour = localDateTime.getHour();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute + 1);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        return date1;
    }

    /**
     * 根据两个时间获取相差的月数
     *
     * @param startDate 日期起期
     * @param endDate   日期止期
     * @return
     */
    public static int getMonthDiff(Date startDate, Date endDate) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(startDate);
        c2.setTime(endDate);

        Boolean isSuccess = true;
        Integer monthNum = 0;
        while (isSuccess) {
            monthNum -= 1;
            c2.add(Calendar.MONTH, -1);
            if (c1.compareTo(c2) >= 0) {
                isSuccess = false;
            }
        }

        Integer monthDiff = (~(monthNum - 1));
        monthDiff -= 1;
        monthDiff = monthDiff > 0 ? monthDiff : 0;
        return monthDiff;
    }

    /**
     * 根据两个日期获取相差年数
     *
     * @param startDate 日期起期
     * @param endDate   日期止期
     * @return
     */
    public static int getYearDiff(Date startDate, Date endDate) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(startDate);
        c2.setTime(endDate);

        Boolean isSuccess = true;
        Integer yearNum = 0;
        while (isSuccess) {
            yearNum -= 1;
            c2.add(Calendar.YEAR, -1);
            if (c1.compareTo(c2) >= 0) {
                isSuccess = false;
            }
        }

        Integer yearDiff = (~(yearNum - 1));
        yearDiff -= 1;
        yearDiff = yearDiff > 0 ? yearDiff : 0;
        return yearDiff;
    }

    public static void main(String[] args) {
        String dateStr = "2023-02-06 15:01:01";
        String dateStr1 = "2023-02-06 16:00:00";
//        String billStartTimeS = DateTimeUtils.getBeforeHourTime(new Date(), 1);
//        billStartTimeS += ":00:00";

        String currentMonthStr = getCurrentMonthStr(new Date());

        int i = dateStr.compareTo(dateStr1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date billStartTime = null;
//        try {
//            billStartTime = new Date();
//            long startTimes = billStartTime.getTime();
//            long endTimes = startTimes + 15 * 60 * 1000;
//            System.out.println(startTimes + "--------------" + endTimes);
//            Date dates = sdf.parse(dateStr);
//            Date date1 = sdf.parse(dateStr1);
//            System.out.println(dates.getTime());
//
//            Date nextHourDate = DateTimeUtils.getCurrentMinuteTime(dates);
//            long datesTime = dates.getTime();
//            long date1Time = date1.getTime();
//
//            long timeDifference = date1Time - datesTime;
//
//            long second = timeDifference / 1000;//计算秒
//            long secondSurplus = second % 60;
//            long minute = second / 60;
//            minute = secondSurplus > 0 ? minute + 1 : minute;
        BigDecimal test = new BigDecimal(22);
        BigDecimal calc = new BigDecimal(24);

        BigDecimal divide = calc.divide(test, 4, BigDecimal.ROUND_HALF_UP);

        System.out.println(0);
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }
        System.out.println("-------------");
    }

    public static boolean isSameDay(String date1, String date2) throws ParseException {
        if (StringUtils.isBlank(date1) || StringUtils.isBlank(date2)) {
            return false;
        }
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = fmt.parse(date1);
        Date d2 = fmt.parse(date2);
        return fmt.format(d1).equals(fmt.format(d2));
    }

    /**
     * 通过秒毫秒数判断两个时间的间隔的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }

}
